package com.crack.nga2.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.crack.nga2.R;
import com.crack.nga2.entity.MessageContent;
import com.crack.nga2.utils.GlobalConst;
import com.crack.nga2.utils.TimeUtils;
import com.crack.nga2.utils.HttpParams;
import com.crack.nga2.volley.MessageContentRequest;

import java.util.ArrayList;
import java.util.List;

public class MessageContentActivity extends BaseActivity {
    private List<MessageContent> contents = new ArrayList<>();
    private MessageContentAdapter adapter = new MessageContentAdapter();
    private int page = 1;
    private boolean nextPage = true;
    private RequestQueue mQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_content);
        mQueue = NgaApplication.getQueue();
        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(adapter);
        getData();
    }

    private void getData() {
        HttpParams params = new HttpParams();
        params.put("__lib", "message");
        params.put("__act", "message");
        params.put("act", "read");
        params.put("mid", getIntent().getIntExtra("mid", -1));
        params.put("page", page);
        params.put("lite", "js");
        String path = "nuke.php?";
        String url = GlobalConst.BASE_URL + path + params.toString();
        mQueue.add(new MessageContentRequest(Request.Method.GET, url,
                new Response.Listener<List<MessageContent>>() {
                    @Override
                    public void onResponse(List<MessageContent> response) {
                        if (response.size() == 0) {
                            Toast.makeText(MessageContentActivity.this, "已经是最后一页", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (page == 1) {
                            contents.clear();
                        }
                        contents.addAll(response);
                        adapter.notifyDataSetChanged();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        nextPage = false;
                    }
                }));
    }


    public void doClick(View view) {
        switch (view.getId()) {
            case R.id.action_return:
                finishForAnimation();
                break;
            case R.id.iv_reply:
                //回复
                break;
        }
    }


    private class MessageContentAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return contents.size();
        }

        @Override
        public Object getItem(int position) {
            return contents.get(position);
        }

        @Override
        public long getItemId(int position) {
            return contents.get(position).getId();
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            ViewHolder holder;
            if (view == null) {
                holder = new ViewHolder();
                view = View.inflate(MessageContentActivity.this, R.layout.activity_message_content_item, null);
                holder.content = (TextView) view.findViewById(R.id.content);
                holder.subject = (TextView) view.findViewById(R.id.subject);
                holder.from_username = (TextView) view.findViewById(R.id.user_name);
                holder.time = (TextView) view.findViewById(R.id.time);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            MessageContent content = contents.get(position);
            holder.from_username.setText(content.getUsername());
            holder.subject.setText(content.getSubject());
            holder.content.setText(Html.fromHtml(content.getContent()));
            holder.time.setText(TimeUtils.formatStandardTime(content.getTime() * 1000));
            return view;
        }
    }

    private class ViewHolder {
        TextView subject, from_username, content, time;
    }
}
